package com.tbit.main.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.tbit.main.constant.BrandRemoteConfigConstant;
import com.tbit.main.constant.DaoParamConstant;
import com.tbit.main.constant.RedisConstant;
import com.tbit.main.dao.core.BrandRemoteConfigDao;
import com.tbit.main.pojo.BrandConfigBO;
import com.tbit.main.pojo.BrandRemoteConfig;
import com.tbit.main.service.BrandRemoteConfigService;
import com.tbit.main.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @author LMY
 * @create 2021-07-12 16:39
 */
@Service("brandRemoteConfigService")
public class BrandRemoteConfigServiceImpl implements BrandRemoteConfigService {

    @Autowired
    private BrandRemoteConfigDao brandRemoteConfigDao;
    @Autowired
    private RedisService redisService;

    @Override
    public BrandRemoteConfig getByAccountIdAndType(Integer accountId, Integer configType) {
        Map<String, Object> map = new HashMap<>();
        map.put(DaoParamConstant.accountId, accountId);
        map.put("configType", configType);
        return brandRemoteConfigDao.getByAccountIdAndType(map);
    }

    @Override
    public void del(Integer accountId, Integer configType) {
        Map<String, Object> map = new HashMap<>();
        map.put(DaoParamConstant.accountId, accountId);
        map.put("configType", configType);
        brandRemoteConfigDao.del(map);
        redisService.del(RedisConstant.BRANDREMOTECONFIG_KEY_PREFIX + accountId);
    }

    @Override
    public void insert(BrandRemoteConfig brandRemoteConfig) {
        brandRemoteConfigDao.insert(brandRemoteConfig);
    }

    @Override
    public BrandConfigBO getConfigTbit(Integer accountId) {
        JSONObject jsonObject = this.getBrandConfigJson(accountId, BrandRemoteConfigConstant.TBIT_CONFIG);
        if (jsonObject == null) {
            return null;
        }

        String appId = jsonObject.getString("appId");
        String nonce = jsonObject.getString("nonce");
        String appSecret = jsonObject.getString("appSecret");
        return new BrandConfigBO(appId, nonce, appSecret);

    }

    @Override
    public JSONObject getBrandConfigJson(Integer accountId, Integer configType) {
        BrandRemoteConfig brandRemoteConfig = this.getByAccountIdAndType(accountId, configType);
        if (brandRemoteConfig == null) {
            return null;
        }
        JSONObject jsonObject = new JSONObject();
        String key = brandRemoteConfig.getParamKV();
        String[] strings = key.split("&");
        for (String s : strings) {
            jsonObject.put(s.substring(0, s.indexOf("=")), s.substring(s.indexOf("=") + 1));
        }
        return jsonObject;
    }

    @Override
    public Integer getTemplateIdTbit(Integer accountId, Integer configType) {
        BrandRemoteConfig remoteConfig = this.getByAccountIdAndType(accountId, configType);
        if (remoteConfig == null) {
            return null;
        }
        return Integer.valueOf(remoteConfig.getParamKV().substring(remoteConfig.getParamKV().indexOf("=") + 1));
    }
}
